package main

import (
	"encoding/json"
	"fmt"
)

type S1 struct {
	Name    string
	Age     int
	Score   []int
	Address string
}

func main() {
	stu := S1{
		Name:    "法师",
		Age:     12,
		Score:   []int{21, 12},
		Address: "成都",
	}

	//生成格式化json数据格式
	sli, _ := json.Marshal(stu)

	Stu1 := new(S1)
	//json反序列化为结构体
	err := json.Unmarshal(sli, Stu1)
	if err != nil {
		fmt.Println("反序列化失败", err)
	} else {
		//fmt.Println(Stu1.Name)
	}

	/***********反序列化为map************/
	m := make(map[string]interface{})
	m["Name"] = "法师"
	m["Age"] = 12
	m["Score"] = []int{12, 34}
	m["Address"] = "成都"

	//通过map转换出来的json是无序的
	s, err := json.Marshal(m)

	m1 := make(map[string]interface{})
	err = json.Unmarshal(s, &m1)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(m1)
	}
	/***********反序列化为map************/
}
